package com.shouhe.mysap.util;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.shouhe.mysap.common.MySapResult;
import com.shouhe.mysap.entity.SaleItem;
import com.shouhe.mysap.entity.TechBom;

import java.util.Collection;
import java.util.List;

/**
 * @author 守鹤
 * @date 2020/6/1 22:55
 */
public class SelectUtil {

    /**
     * 通过字段的相等，查询一条数据，要求字段不能重复
     * @param colum
     * @param object
     * @param service
     * @param <T>
     * @return
     */
    public static  <T> T selectOneEq(String colum,Object object,IService<T> service){
        QueryWrapper<Object> wrapper = new QueryWrapper<>();
        wrapper.eq(colum,object);
        T one = service.getOne((Wrapper<T>) wrapper);
        return one;
    }


    /**
     * 通过字段的相等，查询多条数据
     * @param colum
     * @param object
     * @param service
     * @param <T>
     * @return
     */
    public static  <T> List<T> selectListEq(String colum,Object object,IService<T> service){
        QueryWrapper<T> wrapper = new QueryWrapper<>();
        wrapper.eq(colum,object);
        return service.list(wrapper);
    }

    /**
     * 通过字段的包含，查询多条数据
     * @param colum
     * @param collection
     * @param service
     * @param <T>
     * @return
     */
    public static  <T> List<T> selectListIn(String colum, Collection collection, IService<T> service){
        QueryWrapper<T> wrapper = new QueryWrapper<>();
        wrapper.in(colum,collection);
        return service.list(wrapper);
    }


    /**
     * 查询商品总价
     * @param service
     * @param <T>
     * @return
     */
    public static <T> Double selectSaleItemPrice(String itemCode,IService<T> service){
        List<T> item_code = SelectUtil.selectListEq("item_code", itemCode, service);
        Double sum=0d;
        for (T t : item_code) {
            TechBom bom=(TechBom)t;
            Integer useCnt = bom.getUseCnt();
            Double price = bom.getThingPrice();
            //如果有一个价格没有，直接返回结果 -1
            if(price==-1){
                return -1d;
            }
            sum+=useCnt*price;
        }
        return sum;
    }


}
